<template>
    <div id="app">

        <div class="Buttons">
            <div class="fancy" @click="isTopology = !isTopology">
                <span class="top-key"></span>
                <span class="text"> Switch Views</span>
                <span class="bottom-key-1"></span>
                <span class="bottom-key-2"></span>
            </div>
            <div class="fancy" v-show="isTopology == true" @click="curPieID = (curPieID + 1) % client_num">
                <span class="text"> Switch Pie Charts</span>
            </div>
            
        </div>

        <div class="toggler" v-show="isTopology == true">
            <!-- 注意这里的v-model，控制饼图展示与否 -->
            <input id="toggler-1" v-model="showPie" name="toggler-1" type="checkbox" value="1">
            <label for="toggler-1">
                <svg class="toggler-on" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 130.2 130.2">
                    <polyline class="path check" points="100.2,40.2 51.5,88.8 29.8,67.5"></polyline>
                </svg>
                <svg class="toggler-off" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 130.2 130.2">
                    <line class="path line" x1="34.4" y1="34.4" x2="95.8" y2="95.8"></line>
                    <line class="path line" x1="95.8" y1="34.4" x2="34.4" y2="95.8"></line>
                </svg>
            </label>
        </div>
        
        <Topology v-if="isTopology == true" 
            :network_layers="network_layers" 
            :client_num="client_num"
            :client_layers_arr="client_layers_arr" 
            :feat_size="feat_size" 
            :time="time" 
        />

        <!-- 这里是用 v-for 布局，把所有饼图都展示，但是屏幕装不下所以换了主意 -->
        <!-- <ul id="TimePie" v-if="isTopology == true">
            <TimePie v-for="(item, index) in time" :key="index"
                :client_time="item" 
                :client_id="index + 1"
            />
        </ul> -->

        <TimePie v-if="isTopology == true && showPie == true " :class="TimePieStyle"
            :client_time="time[curPieID]" 
            :client_id="curPieID + 1" 
        />
        

        <ClientAcc v-if="isTopology == false" :client_info="client_info" />

    </div>

</template>
  
<script>
import axios from 'axios'
import Topology from './components/Topology.vue'
import TimePie from './components/TimePie.vue'
// import Client from './components/Client.vue'
import ClientAcc from './components/ClientAcc.vue'

export default {
    name: 'App',
    components: { Topology, TimePie, ClientAcc },
    data() {
        return {
            isTopology: true,
            showPie: true,
            curPieID: 0,  //当前饼图显示的clientID
            TimePieStyle: 'Time_Pie',  //指定饼图的css

            //这里的数据只是假数据，目的是为了看清楚这些数据的格式

            network_layers: 10,
            client_num: 4,
            client_layers_arr: [3, 6, 4, 9],

            feat_size: ["[32, 1024]", "[32, 256, 8, 8]"],
            time: [{
                "Client-0": 65714761.86336608,
                "Client-1": 81927512.65067351,
                "Client-2": 49560469.8537196,
                "Client-3": 152756860.03438517,
                "Client-4": 60674013.23117779,
                "Client-5": 805946.266524,
                "Client-6": 146951.0505281,
                "Network": 3661824.7199758445,
                "Server-7": 299557.9475233531
            }],

            //这里之所以这么丑陋，是因为之前折线图的代码已经写好了，然后接口的数据格式变了。。。
            client_info: [{  
                "acc": [
                    0.1,
                    0.2,
                    0.3
                ],
            }],

        }
    },
    created() {

        //读取静态设置
        axios.get('./setting.json', {}).then(value => {
            // console.log('/setting.json #', value.data)
            this.network_layers = value.data.n_layers_total
            this.client_num = value.data.client_num
        });

        //读取动态数据
        let getData = () => {
            axios.get('./client_info.json', {}).then(value => {
                // console.log('/client_info.json #', value.data)
                let n_layer = []
                let feat_size = []
                let time = []
                let client_info = []

                for (let i = 1; i <= this.client_num; ++i) {
                    n_layer.push(value.data[String(i)].n_layer)
                    feat_size.push(value.data[String(i)].feat_size)
                    time.push(value.data[String(i)].time)

                    //增量获取数据，必须先判断 client_info[i - 1] 是否为 undefined
                    if (this.client_info[i - 1]) {  
                        let existingData = this.client_info[i - 1].acc
                        let totalData = existingData.concat(value.data[String(i)].accuracy)
                        client_info.push({ "acc": totalData })
                    } else {
                        client_info.push({ "acc": value.data[String(i)].accuracy })
                    }
                }

                this.client_layers_arr = n_layer
                this.feat_size = feat_size
                this.time = time
                this.client_info = client_info

                console.log('app中的数据 #', this._data)

            });
        }
        getData()  //立即执行一次
        this.timer = setInterval(getData, 4000)  //开启获取数据的定时器

        // let getRamdon = () => {
        //     axios.get('http://127.0.0.1:5000/accuracy', {}).then(value => {
        //         //console.log(value.data)
        //         this.client_info.forEach(item => {
        //             // console.log(value.data.Accuracy)
        //             item.acc = value.data.Accuracy  //离谱，这里居然还有个.Accuracy，de这个bug一通宵
        //         });
        //     });
        // }
        // getRamdon()
        // this.timer2 = setInterval(getRamdon, 5000)  //从后端每x毫秒获取一次数据

    },
    beforeDestroy() {
        //clearInterval(this.timer)
        clearInterval(this.timer)
        console.log('app即将驾鹤西游了')
    },
}
</script>
  
<style>
#app {
    margin: 10px;
}

.Time_Pie{
    list-style: none;
    position: absolute;

    top: 360px;
    left: 800px;

    /* 这里是之前用 v- for 的时候开启了flex布局，但是发现效果不太好看 */
    /* width: 1050px; 
    display: flex;
    flex: auto; 
    justify-content: center;*/

    padding-top: 50px;
    padding-left: 60px;

    border:1px solid #bfd1eb;
    background:#f3faff;
    box-shadow: 0px 0px 5px rgba(0, 0, 0, .3);
    
}

/* #TimePie > li {
    padding: 0;
    margin: 0;
    list-style: none
} */

.Buttons{
    display: flex;
    justify-content: space-between;
    padding: 0px 20px;
}

/* 调整右边按钮的右边距 */
.Buttons > div:last-child{
    margin-right: 50px;
}

.fancy {
    background-color: transparent;
    border: 2px solid #000;
    border-radius: 0;
    box-sizing: border-box;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    /* float: right; */
    font-weight: 700;
    letter-spacing: 0.05em;
    margin: 0;
    outline: none;
    overflow: visible;
    padding: 1.25em 2em;
    position: relative;
    text-align: center;
    text-decoration: none;
    text-transform: none;
    transition: all 0.3s ease-in-out;
    user-select: none;
    /* 调整按钮大小 */
    font-size: 8px;
}

.fancy::before {
    content: " ";
    width: 1.1rem;
    height: 2px;
    background: black;
    top: 50%;
    left: 1em;
    /* 调整黑线的定位  */
    position: absolute;
    transform: translateY(-50%);
    transform-origin: center;
    transition: background 0.3s linear, width 0.3s linear;
}

.fancy .text {
    font-size: 1.125em;
    line-height: 1.33333em;
    padding-left: 2em;
    display: block;
    text-align: left;
    transition: all 0.3s ease-in-out;
    text-transform: uppercase;
    text-decoration: none;
    color: black;
}

.fancy .top-key {
    height: 2px;
    width: 1.5625rem;
    top: -2px;
    left: 0.625rem;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, left 0.3s ease-out;
}

.fancy .bottom-key-1 {
    height: 2px;
    width: 1.5625rem;
    right: 1.875rem;
    bottom: -2px;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, right 0.3s ease-out;
}

.fancy .bottom-key-2 {
    height: 2px;
    width: 0.625rem;
    right: 0.625rem;
    bottom: -2px;
    position: absolute;
    background: #e8e8e8;
    transition: width 0.5s ease-out, right 0.3s ease-out;
}

.fancy:hover {
    color: white;
    background: black;
}

.fancy:hover::before {
    width: 0.9375rem;
    background: white;
}

.fancy:hover .text {
    color: white;
    padding-left: 1.5em;
}

.fancy:hover .top-key {
    left: -2px;
    width: 0px;
}

.fancy:hover .bottom-key-1,
.fancy:hover .bottom-key-2 {
    right: 0;
    width: 0;
}

/* 框框checkBox！！！ */
.toggler {
    width: 72px;
    margin: 40px auto;

    /* 框框checkBox开启定位 */
    position: absolute;
    top: -30px;
    right: 300px;
}

.toggler input {
    display: none;
}

.toggler label {
    display: block;
    position: relative;
    width: 72px;
    height: 33px;
    border: 1px solid #d6d6d6;
    border-radius: 36px;
    background: #e4e8e8;
    cursor: pointer;
}

.toggler label::after {
    display: block;
    border-radius: 100%;
    background-color: #d7062a;
    content: '';
    animation-name: toggler-size;
    animation-duration: 0.15s;
    animation-timing-function: ease-out;
    animation-direction: forwards;
    animation-iteration-count: 1;
    animation-play-state: running;
}

.toggler label::after,
.toggler label .toggler-on,
.toggler label .toggler-off {
    position: absolute;
    top: 50%;
    left: 25%;
    width: 26px;
    height: 26px;
    transform: translateY(-50%) translateX(-50%);
    transition: left 0.15s ease-in-out, background-color 0.2s ease-out, width 0.15s ease-in-out, height 0.15s ease-in-out, opacity 0.15s ease-in-out;
}

.toggler input:checked+label::after,
.toggler input:checked+label .toggler-on,
.toggler input:checked+label .toggler-off {
    left: 75%;
}

.toggler input:checked+label::after {
    background-color: #50ac5d;
    animation-name: toggler-size2;
}

.toggler .toggler-on,
.toggler .toggler-off {
    opacity: 1;
    z-index: 2;
}

.toggler input:checked+label .toggler-off,
.toggler input:not(:checked)+label .toggler-on {
    width: 0;
    height: 0;
    opacity: 0;
}

.toggler .path {
    fill: none;
    stroke: #fefefe;
    stroke-width: 7px;
    stroke-linecap: round;
    stroke-miterlimit: 10;
}

@keyframes toggler-size {

    0%,
    100% {
        width: 26px;
        height: 26px;
    }

    50% {
        width: 20px;
        height: 20px;
    }
}

@keyframes toggler-size2 {

    0%,
    100% {
        width: 26px;
        height: 26px;
    }

    50% {
        width: 20px;
        height: 20px;
    }
}
</style>
  
